package com.fxkj.cloud.drms.application.impl;

import com.fxkj.cloud.dbs.common.model.DeviceAttributeKey;
import com.fxkj.cloud.drms.application.CommandSettingHistoryApplicationService;
import com.fxkj.cloud.drms.application.converter.CommandSettingHistoryApplicationConverter;
import com.fxkj.cloud.drms.application.model.vo.CommandSettingHistoryModelView;
import com.fxkj.cloud.drms.application.model.vo.LatestCommandSettingHistoryKvModelView;
import com.fxkj.cloud.drms.domain.commandSettingHistory.service.CommandSettingHistoryDomainService;
import com.fxkj.cloud.drms.domain.device.repository.DeviceRepository;
import com.fxkj.common.exception.FxErrorCode;
import com.fxkj.common.exception.FxException;
import org.springframework.data.web.PagedModel;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

/**
 * <p>description: </p>
 *
 * @author ww
 * @date 2025-03-25 11:06:52
 */
@Service
public class CommandSettingHistoryApplicationServiceImpl implements CommandSettingHistoryApplicationService {

    private final CommandSettingHistoryDomainService commandSettingHistoryDomainService;
    private final DeviceRepository deviceRepository;
    private final CommandSettingHistoryApplicationConverter commandSettingHistoryApplicationConverter;

    public CommandSettingHistoryApplicationServiceImpl(CommandSettingHistoryDomainService commandSettingHistoryDomainService, DeviceRepository deviceRepository, CommandSettingHistoryApplicationConverter commandSettingHistoryApplicationConverter) {
        this.commandSettingHistoryDomainService = commandSettingHistoryDomainService;
        this.deviceRepository = deviceRepository;
        this.commandSettingHistoryApplicationConverter = commandSettingHistoryApplicationConverter;
    }

    @Override
    public PagedModel<CommandSettingHistoryModelView> page(Integer page, Integer size, UUID deviceId, Integer attributeType, Long startTime, Long endTime, String deviceName, String username, UUID organizationId) {
        return new PagedModel<>(commandSettingHistoryDomainService.page(page, size, deviceId, attributeType, startTime, endTime, deviceName, username, organizationId).map(commandSettingHistoryApplicationConverter::toView));
    }

    @Override
    public List<LatestCommandSettingHistoryKvModelView> getLatestCommandSettingHistoryKv(UUID deviceId, List<String> attributeKeys, UUID organizationId) throws FxException {
        List<DeviceAttributeKey> deviceAttributeKeys = deviceRepository.getDeviceAttributeKeys(deviceId, new HashSet<>(attributeKeys));
        if (deviceAttributeKeys.isEmpty()) {
            throw new FxException("设备属性不存在", FxErrorCode.GENERAL);
        }
        List<String> keys = deviceAttributeKeys.stream().map(DeviceAttributeKey::getKey).toList();
        if (keys.isEmpty()) {
            throw new FxException("设备属性不存在", FxErrorCode.GENERAL);
        }
        return commandSettingHistoryApplicationConverter.toLatestView(commandSettingHistoryDomainService.getLatestCommandSettingHistoryKv(deviceId, keys, organizationId));
    }

}
